<template>
  <div class="lzc-select">
    <input
      ref="input"
      class="lzc-input"
      type="text"
      v-model="inputVal"
      @focus="showContent = true"
    />
    <div id="content" v-show="showContent" @click="selectItem" @scroll="handleScroll">
      <div class="real-box" :style="realBoxStyle"></div>
      <div class="view-box" :style="viewBoxStyle">
        <div
          v-for="item in list"
          :key="item.value"
          :class="['item', inputVal === item.label ? 'item-select' : '']"
        >
          {{ item.label }}
        </div>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ref, watchEffect, onMounted, getCurrentInstance, onUnmounted, watch, computed } from 'vue'
interface ListItem {
  label: string
  value: string
}
const input = ref()
const inputVal = ref()
const value = ref('')
const list = ref<ListItem[]>([])
const props = defineProps(['valFromParent', 'listFromParent'])
watchEffect(() => {
  if (props.valFromParent && props.listFromParent?.length) {
    const listItem = props.listFromParent.find(
      (item: { value: any }) => item.value === props.valFromParent
    )
    if (listItem) {
      inputVal.value = listItem.label || ''
    }
  }
})
const totalHeight = ref(0)
watch(
  () => props.listFromParent,
  () => {
    totalHeight.value = 30 * props.listFromParent.length
    loadList()
  }
)
watch(
  () => props.valFromParent,
  () => {
    value.value = props.valFromParent
  }
)
const realBoxStyle = computed(() => {
  return 'height:' + totalHeight.value + 'px'
})
const scrollTop = ref(0)
const viewBoxStyle = computed(() => {
  return 'top:' + scrollTop.value + 'px'
})
const showContent = ref(false)
const proxy = getCurrentInstance()
const hideContent = (e: { target: unknown }) => {
  if (e.target !== proxy?.proxy?.$refs.input) {
    showContent.value = false
  }
}
const selectItem = (e) => {
  const label = e.target.innerHTML
  inputVal.value = label
  const listItem = list.value.find((item) => item.label === label)
  value.value = listItem ? listItem.value : ''
}
const handleScroll = (e) => {
  requestAnimationFrame(() => {
    scrollTop.value = e.target.scrollTop
    const index = Math.floor(scrollTop.value / 30)

    list.value = props.listFromParent.slice(index, index + 10)
    console.log(list.value)
  })
}
const loadList = () => (list.value = props.listFromParent.slice(0, 10))

onMounted(() => {
  window.addEventListener('click', hideContent)
  loadList()
})
onUnmounted(() => {
  window.removeEventListener('click', hideContent)
})
</script>
<style lang="scss" scoped>
.lzc-select {
  position: relative;
}
.lzc-input {
  width: 100%;
  height: 30px;
}
#content {
  position: absolute;
  top: 40px;
  left: 0;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  overflow: auto;
  width: 100%;
  height: 300px;
  .real-box {
    width: calc(100% - 15px);
    height: 0;
  }
  .view-box {
    position: absolute;
    width: 100%;
    left: 0;
    .item {
      width: 100%;
      background-color: aliceblue;
      height: 30px;
      line-height: 30px;
      padding-left: 10px;
      &.item-select {
        background-color: aqua;
      }
      &:hover {
        background-color: antiquewhite;
      }
    }
  }
}
</style>
